package com.sunday.fangdaifu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.widgets.banner.ConvenientBanner;
import com.sunday.common.widgets.banner.holder.ViewHolderCreator;
import com.sunday.fangdaifu.R;
import com.sunday.fangdaifu.activity.common.WebViewActivity;
import com.sunday.fangdaifu.common.Api;
import com.sunday.fangdaifu.common.ApiClient;
import com.sunday.fangdaifu.model.DateBanner;
import com.sunday.fangdaifu.model.DateContent;
import com.sunday.fangdaifu.model.DateType;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yun_sheng on 2017/6/29.
 */

public class DateListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private List<DateType> dataSet;
    private View.OnClickListener onClickListener;


    public DateListAdapter(Context context) {
        mContext = context;

    }

    public void setData(List<DateType> datas) {
        dataSet = datas;
    }

    @Override
    public int getItemViewType(int position) {
        return dataSet.get(position).getType();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 2) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_banner, parent, false);
            return new TopViewHolder(v);
        } else {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_date, parent, false);
            return new ItemViewHolder(v);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof TopViewHolder) {
            final List<DateBanner> date = dataSet.get(position).getDateBannerList();
            TopViewHolder viewHolder1 = (TopViewHolder) holder;
            viewHolder1.banner.setPages(viewHolder1.viewHolderCreator, date)
                    .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT)
                    .setPageIndicator(new int[]{R.mipmap.ic_page_indicator, R.mipmap.ic_page_indicator_focused});
            viewHolder1.banner.notifyDataSetChanged();

        } else {
//            List<DateContent> contents=new ArrayList<>();
//            contents.addAll(dataSet.get(position).getDateContentList());
            ItemViewHolder viewHolder = (ItemViewHolder) holder;
            viewHolder.item.removeAllViews();
            for (final DateContent content :dataSet.get(position).getDateContentList()) {
                View v1 = LayoutInflater.from(mContext).inflate(R.layout.item_date_child, null);
                ImageView img = (ImageView) v1.findViewById(R.id.img);
                TextView title = (TextView) v1.findViewById(R.id.title);
                TextView time = (TextView) v1.findViewById(R.id.time);
                RelativeLayout itemLayout= (RelativeLayout) v1.findViewById(R.id.item);
                if (content.getTitle() != null)
                    title.setText(content.getTitle());
                if (content.getCreateTime() != null)
                    time.setText(content.getCreateTime());
                if (!TextUtils.isEmpty(content.getImage())) {
                    Picasso.with(mContext)
                            .load(content.getImage())
                            .error(R.mipmap.ic_launcher)
                            .into(img);
                }
                itemLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = null;
                        intent = new Intent(mContext, WebViewActivity.class);
                        intent.putExtra("title","资讯详情");
                        intent.putExtra("url", ApiClient.URL + content.getId());
                        mContext.startActivity(intent);
                    }
                });
                viewHolder.item.addView(v1);
            }

        }
    }

    @Override
    public int getItemCount() {
        return dataSet == null ? 0 : dataSet.size();
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item)
        LinearLayout item;

        public ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public class TopViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.banner)
        ConvenientBanner banner;
        private ViewHolderCreator viewHolderCreator;
        private DateBannerHolder bannerViewHolder;

        public TopViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            banner.startTurning(3000);
            bannerViewHolder = new DateBannerHolder();
            viewHolderCreator = new ViewHolderCreator<DateBannerHolder>() {

                @Override
                public DateBannerHolder createHolder() {
                    return bannerViewHolder;
                }
            };
            bannerViewHolder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DateBanner item = (DateBanner) v.getTag();
                    Intent intent = null;
                    intent = new Intent(mContext, WebViewActivity.class);
                    intent.putExtra("title","资讯详情");
                    intent.putExtra("url", ApiClient.URL + item.getId());
                    mContext.startActivity(intent);
                }
            });
        }
    }


}
